package com.thememaker.lockstyle.parsii;

import com.thememaker.lockstyle.bean.ui.BaseView;
import com.thememaker.lockstyle.bean.ui.Image;
import com.thememaker.lockstyle.bean.ui.Wallpaper;
import com.thememaker.lockstyle.control.LayoutComponentUtils;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

public class LockscreenViewPaesiiUtils {

    private static LockscreenViewPaesiiUtils lockscreenViewPaesiiUtils;

    private ExpressionUtils expressionUtils;

    public void setExpressionUtils(ExpressionUtils expressionUtils) {
        this.expressionUtils = expressionUtils;
    }

    public LockscreenViewPaesiiUtils (){

    }

    public static LockscreenViewPaesiiUtils getInstance(ExpressionUtils expressionUtils){
        if (lockscreenViewPaesiiUtils == null){
            lockscreenViewPaesiiUtils = new LockscreenViewPaesiiUtils();
            lockscreenViewPaesiiUtils.setExpressionUtils(expressionUtils);
        }
        return lockscreenViewPaesiiUtils;
    }


    public Path paesiiImagePath(Image image,String lockscreenPathString){
        Optional<Path> srcOptional = Optional.ofNullable(image)
                        .flatMap(image1 -> Optional.ofNullable(image1.getSrc()))
                        .map(src->getSrcidPath(src,image,lockscreenPathString));
        Optional<Path> sourcesOptional = Optional.ofNullable(image)
                .flatMap(image1 -> Optional.ofNullable(image1.getSourcesAnimation()))
                .flatMap(sourcesAnimation -> Optional.ofNullable(sourcesAnimation.getSourceList()))
                .filter(sources -> sources.size() > 0)
                .flatMap(sources -> Optional.ofNullable(sources.get(0).getSrc()))
                .map(source -> Paths.get(lockscreenPathString, source));
        return srcOptional.orElse(sourcesOptional.orElse(null));
//        Path path = null;
//        if (image.getSrc() != null) {
//            path = Paths.get(lockscreenPathString, image.getSrc());
//            if (image.getSrcid() != null) {
//                path = Paths.get(path.toString()
//                        .replace(".png", "_" + (int) expressionUtils.parseExpr(image.getSrcid()) + ".png")
//                        .replace(".jpg", "_" + (int) expressionUtils.parseExpr(image.getSrcid()) + ".jpg"));
//            }
//        } else {
//            if (image.getSourcesAnimation() != null && image.getSourcesAnimation().getSourceList() != null
//                    && image.getSourcesAnimation().getSourceList().size() > 0) {
//                path = Paths.get(lockscreenPathString, image.getSourcesAnimation().getSourceList().get(0).getSrc());
//            }
//        }
//        return path;
    }



    public Path paesiiWallpaperPath(Wallpaper wallpaper, String lockscreenPathString,double height){
        Path path = null;
        if (wallpaper.getSrc() != null) {
            path = Paths.get(lockscreenPathString, wallpaper.getSrc());
        } else {
           path = Paths.get(lockscreenPathString,height>1920 ? "default_lock_wallpaper"+ height+".jpg":"default_lock_wallpaper.jpg");
        }
        return path;
    }

    private Path getSrcidPath(String src,Image image,String lockscreenPathString){
        return Optional.ofNullable(image.getSrcid())
                .map(srcid ->
                        Paths.get(lockscreenPathString, src
                                .replace(".png", "_" + (int) expressionUtils.parseExpr(image.getSrcid()) + ".png")
                                .replace(".jpg", "_" + (int) expressionUtils.parseExpr(image.getSrcid()) + ".jpg")
                        )
                ).orElse(Paths.get(lockscreenPathString, src));
    }

}
